<template>
  <div class="advert">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <el-button @click="$router.push({ name: 'advert-create' })" size="small">添加广告</el-button>
      </div>
      <el-table
        :data="adList"
        v-loading="loading"
        style="width: 100%">
        <el-table-column label="编号" min-width="150" type="index"></el-table-column>
        <el-table-column prop="name" label="广告名称" min-width="150"></el-table-column>
        <el-table-column prop="spaceName" label="广告位置" min-width="150"></el-table-column>
        <el-table-column prop="icon" label="广告图片" min-width="150">
          <el-image :src="scope.row.img" slot-scope="scope">
            <div slot="error" class="image-slot">
            <i class="el-icon-picture-outline"></i>
          </div>
        </el-image>
        </el-table-column>
        <el-table-column prop="orderNum" label="时间" min-width="150">
          <template slot-scope="scope">
            <div>开始时间：<span>{{scope.row.startTimeFormatString}}</span></div>
            <div>到期时间：<span>{{scope.row.endTimeFormatString}}</span></div>
          </template>
        </el-table-column>
        <el-table-column label="操作" min-width="150">
          <template slot-scope="scope">
            <el-button size="mini" @click="handleEdit(scope.row)">编辑</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
  </div>
</template>

<script lang="ts">
import Vue from 'vue'
import { getAllAds, getAllSpaces } from '@/services/ads'

export default Vue.extend({
  name: 'MenuIndex',
  data () {
    return {
      adList: [],
      loading: true
    }
  },

  created () {
    this.loadAllAds()
  },

  methods: {
    async loadAllAds () {
      const { data } = await getAllAds()
      if (data.success === true) {
        const { data: spaceData } = await getAllSpaces()
        this.adList = data.content.map((item: any) => {
          const space = spaceData.content.find((space: any) => {
            return space.id === item.spaceId
          })
          if (space) {
            item.spaceName = space.name
          }
          return item
        })
        this.loading = false
      }
    },

    handleEdit (item: any) {
      this.$router.push({
        name: 'advert-edit',
        params: {
          id: item.id
        }
      })
    }
  }
})
</script>

<style lang="scss">
.advert{
  .el-table{
    th,td{
      text-align: center;
    }
  }
  .el-card{
    .el-card__body{
      padding-top: 0;
    }
  }
  .el-image{
    width: 100px;
    height: 100px;
    font-size: 25px;
    margin: 0 auto;
    display: flex;
    align-items: center;
    justify-content: center;
    background-color: rgba(0,0,0,0.03);

    .el-image__inner{
      height: auto;
    }
    img{
      height: auto;
    }
  }
}

</style>
